import { fetchAddressListAPI, fetchEmployeeListAPI, fetchInstitutionDetailAPI, fetchInstitutionTreeAPI, updateInstitutionAPI } from '@/api/essentialDataAPI/organizationAPI'

const actions = {
  /*
  * 获取树状机构信息的action
  *  */
  async fetchInstitutionTreeAction(context) {
    const { data } = await fetchInstitutionTreeAPI()
    return JSON.parse(data)
  },

  /*
  * 获取员工分页数据的action
  * */
  async fetchEmployeeListAction(context, pageParams) {
    const { data } = await fetchEmployeeListAPI(pageParams)
    context.commit('setEmployeeListMutation', data)
  },

  /*
  * 获取机构详情的action
  *  */
  async fetchInstitutionDetailAction(context, id) {
    const { data } = await fetchInstitutionDetailAPI(id)
    return data
  },

  /*
  *根据父id获取地址信息的action
  *  */
  async fetchAddressListAction(context, parentId) {
    const { data } = await fetchAddressListAPI(parentId)
    return data
  },

  /*
  * 编辑机构信息的action
  *  */
  async updateInstitutionAction(context, formData) {
    await updateInstitutionAPI(formData)
    return '编辑机构信息成功'
  }
}
const mutations = {
  /*
  * 设置员工分页数据的mutation
  *  */
  setEmployeeListMutation(state, { counts, items }) {
    state.employeeList = items
    state.counts = Number(counts)
  }

}
const state = () => {
  return {
    employeeList: [],
    counts: 0
  }
}

export default {
  actions,
  mutations,
  state,
  namespaced: true
}
